package cn.tedu.staff_user.management.service.impl;

import cn.tedu.common.enumerator.ServiceCode;
import cn.tedu.common.ex.ServiceException;
import cn.tedu.staff_user.management.dao.persist.repository.IStaffRepository;
import cn.tedu.staff_user.management.dao.persist.repository.IUserRepository;
import cn.tedu.staff_user.management.pojo.entity.Staff;
import cn.tedu.staff_user.management.pojo.entity.User;
import cn.tedu.staff_user.management.pojo.param.StaffAddNewParam;
import cn.tedu.staff_user.management.pojo.param.StaffUpdateParam;
import cn.tedu.staff_user.management.pojo.param.UserAddNewParam;
import cn.tedu.staff_user.management.pojo.param.UserUpdateParam;
import cn.tedu.staff_user.management.pojo.vo.StaffListItemVO;
import cn.tedu.staff_user.management.pojo.vo.UserListItemVO;
import cn.tedu.staff_user.management.pojo.vo.UserStandardVO;
import cn.tedu.staff_user.management.service.IStaffService;
import cn.tedu.staff_user.management.service.IUserService;
import com.baomidou.mybatisplus.extension.service.IService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserRepository userRepository;

    /*@Override
    public List<UserListItemVO> list() {
        return userRepository.list();
    }*/

    @Override
    public List<User> list() {
        return userRepository.list();
    }

    @Autowired
    PasswordEncoder encoder;

    @Override
    public void addNew(UserAddNewParam userAddNewParam) {

        String username = userAddNewParam.getUsername();
        log.error(username);
        int count = userRepository.countName(username);
        log.error(count + "**********");
        if (count > 0) {
            String message = "添加用户失败，用户名被占用";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }


        User user = new User();
        BeanUtils.copyProperties(userAddNewParam, user);
        user.setPassword(encoder.encode(userAddNewParam.getPassword()));
        user.setCreateTime(LocalDateTime.now());
        user.setEnable(1);
        int i = userRepository.addNew(user);
        if (i != 1) {
            String message = "添加用户失败，服务器忙，请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        int i = userRepository.deleteById(id);
        if (i != 1) {
            String message = "删除用户失败，服务器忙，请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateById(Long id,UserUpdateParam userUpdateParam) {

        User user = new User();
        BeanUtils.copyProperties(userUpdateParam, user);
        int i = userRepository.updateById(user);
        log.error(i+"********"+userUpdateParam);
        if (i!=1) {
            String message = "更新用户失败，服务器忙，请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }


    @Override
    public void setEnable(Long id) {
        log.debug("开始处理【启用类别】的业务，参数：{}", id);
        updateEnableById(id, ENABLE_STATE_ON);
    }

    @Override
    public void setDisable(Long id) {
        log.debug("开始处理【禁用类别】的业务，参数：{}", id);
        updateEnableById(id, ENABLE_STATE_OFF);
    }

    private void updateEnableById(Long id, Integer enable) {
        UserStandardVO currentUser = userRepository.getStandardById(id);
        log.error(id+"sssssssss"+enable+"----"+currentUser);
        if (currentUser == null) {
            String message = ENABLE_STATE_TEXT[enable] + "类别失败，类别数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        if (currentUser.getEnable().equals(enable)) {
            String message = ENABLE_STATE_TEXT[enable] + "类别失败，此类别已经处于" + ENABLE_STATE_TEXT[enable] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        User updateUser = new User();
        updateUser.setId(id);
        updateUser.setEnable(enable);
        int rows = userRepository.updateById(updateUser);
        log.error(rows+"7777777777777");
        if (rows != 1) {
            String message = ENABLE_STATE_TEXT[enable] + "类别失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}

